#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

//调用文件
//const char*exe="otherExe";

int main()
{
  extern char **environ; 
  pid_t id = fork();
  if(id==0)
  {
    printf("before:i am a child pid:%d,ppid:%d\n",getpid(),getppid());

    //这类方法标准写法
    // execl("/usr/bin/ls","ls", "-a", "-l", NULL); 
    
    
    //等价
    //execlp("/usr/bin/ls","ls", "-a", "-l", NULL); 
   // execlp("ls","ls", "-a", "-l", NULL); 

    
   // char *const myargv[] = {
   //   "ls",
   //   "-a",
   //   "-l",
   //   NULL
   // };
   // execvp("ls",myargv);
   // execv("usr/bin/ls",myargv);
    //如果没有执行excel则执行如下语句，反之则不执行  
   
    
    //调用其它文件
    //execl("./otherExe","otherExe",NULL);
    //execl("/usr/bin/bash","bash","test.sh",NULL);      //可执行文件解释器bash，test.sh脚本
    
    char *const myenv[]={
      "MYVAL=1122334",
      "MYPATH=/usr/bin/XXX",
      NULL 
    };
    execle("./otherExe","otherExe","-a","-w","-v",NULL,myenv);    //覆盖
    printf("before:i am a parent pid:%d,ppid:%d\n",getpid(),getppid());
    exit(0);
  }
  pid_t ret = waitpid(id,NULL,0);
  if(ret>0)printf("wait success parent pid:%d,ret:%d\n",getpid(),ret);
   
  return 0;
}
